import { defineStore } from 'pinia'
import { ref } from 'vue'
import { employeeApi } from '@/axios/api/employees'
import { ElMessage } from 'element-plus'

// 定义员工接口
export interface Employee {
  employee_id: string
  name: string
  affiliation: string
  dept1: string
  dept2: string
  dept3: string
  position: string
  hire_date: string
  leave_date?: string
}

// 创建员工Store
export const useEmployeeStore = defineStore('employee', () => {
  // 状态
  const employees = ref<Employee[]>([])
  const loading = ref(false)
  const currentEmployee = ref<Employee | null>(null)

  // 获取所有员工
  const fetchEmployees = async () => {
    loading.value = true
    try {
      console.log('开始获取员工列表')
      const response = await employeeApi.getEmployees() as unknown as Employee[]
      console.log('获取员工列表响应:', response)
      employees.value = response
    } catch (error) {
      console.error('获取员工列表失败:', error)
      ElMessage.error('获取员工列表失败')
    } finally {
      loading.value = false
    }
  }

  // 获取单个员工
  const fetchEmployee = async (id: string) => {
    loading.value = true
    try {
      console.log(`开始获取员工 ${id}`)
      const response = await employeeApi.getEmployees() as unknown as Employee[]
      const employee = response.find(e => e.employee_id === id)
      if (employee) {
        currentEmployee.value = employee
        console.log(`获取员工 ${id} 成功:`, employee)
        return employee
      }
      return null
    } catch (error) {
      console.error(`获取员工 ${id} 失败:`, error)
      ElMessage.error(`获取员工 ${id} 失败`)
      return null
    } finally {
      loading.value = false
    }
  }

  // 创建员工
  const createEmployee = async (employee: Employee) => {
    loading.value = true
    try {
      console.log('开始创建员工:', employee)
      const response = await employeeApi.createEmployee(employee) as unknown as Employee
      console.log('创建员工响应:', response)
      ElMessage.success('创建员工成功')
      // 不自动刷新列表，由调用者决定是否刷新
      // await fetchEmployees()
      return response
    } catch (error: any) {
      console.error('创建员工失败:', error)
      if (error.response?.data?.detail) {
        ElMessage.error(`创建员工失败: ${error.response.data.detail}`)
      } else {
        ElMessage.error('创建员工失败')
      }
      return null
    } finally {
      loading.value = false
    }
  }

  // 批量创建员工
  const batchCreateEmployees = async (employees: Employee[]) => {
    loading.value = true
    try {
      console.log('开始批量创建员工:', employees)
      const response = await employeeApi.batchCreateEmployees(employees) as unknown as Employee[]
      console.log('批量创建员工响应:', response)
      ElMessage.success('批量创建员工成功')
      // 不自动刷新列表，由调用者决定是否刷新
      // await fetchEmployees()
      return response
    } catch (error: any) {
      console.error('批量创建员工失败:', error)
      if (error.response?.data?.detail) {
        ElMessage.error(`批量创建员工失败: ${error.response.data.detail}`)
      } else {
        ElMessage.error('批量创建员工失败')
      }
      return null
    } finally {
      loading.value = false
    }
  }

  // 更新员工
  const updateEmployee = async (id: string, employee: Partial<Employee>) => {
    loading.value = true
    try {
      console.log(`开始更新员工 ${id}:`, employee)
      const response = await employeeApi.updateEmployee(id, employee) as unknown as Employee
      console.log(`更新员工 ${id} 响应:`, response)
      ElMessage.success('更新员工成功')
      // 不自动刷新列表，由调用者决定是否刷新
      // await fetchEmployees()
      return response
    } catch (error) {
      console.error(`更新员工 ${id} 失败:`, error)
      ElMessage.error(`更新员工 ${id} 失败`)
      return null
    } finally {
      loading.value = false
    }
  }

  // 删除员工
  const deleteEmployee = async (id: string) => {
    loading.value = true
    try {
      console.log(`开始删除员工 ${id}`)
      await employeeApi.deleteEmployee(id)
      console.log(`删除员工 ${id} 成功`)
      ElMessage.success('删除员工成功')
      // 不自动刷新列表，由调用者决定是否刷新
      // await fetchEmployees()
      return true
    } catch (error) {
      console.error(`删除员工 ${id} 失败:`, error)
      ElMessage.error(`删除员工 ${id} 失败`)
      return false
    } finally {
      loading.value = false
    }
  }

  return {
    employees,
    loading,
    currentEmployee,
    fetchEmployees,
    fetchEmployee,
    createEmployee,
    batchCreateEmployees,
    updateEmployee,
    deleteEmployee
  }
}) 